package com.qf.flash.ui.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qf.dblib.dao.entity.ChatUserBean;
import com.qf.dblib.dao.entity.UserInfo;
import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.base.CommonBottomChosenDialog;
import com.qf.flash.constant.Constant;
import com.qf.flash.constant.FlashCodes;
import com.qf.flash.http.response.ReportTypeBean;
import com.qf.flash.mvp.contract.NewFriendInviteContract;
import com.qf.flash.mvp.contract.ReportContract;
import com.qf.flash.mvp.contract.UserInfoContract;
import com.qf.flash.mvp.presenter.NewFriendInvitePresenter;
import com.qf.flash.mvp.presenter.ReportPresenter;
import com.qf.flash.mvp.presenter.UserInfoPresenter;
import com.qf.flash.ui.chat.ChatActivity;
import com.qf.flash.utils.ImageLoader;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.widget.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.qf.flash.constant.Constant.IS_FROM_CHAT;

/**
 * 创建日期：2020/3/10 on 0:21
 * 描述:个人主页 activity
 */
public class MemberMsgActivity extends BaseActivity implements UserInfoContract.View, ReportContract.View, NewFriendInviteContract.View {

    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.iv_level)
    TextView ivLevel;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_mvp)
    TextView tvMvp;
    @BindView(R.id.tv_courage)
    TextView tvCourage;
    @BindView(R.id.tv_wisdom)
    TextView tvWisdom;
    @BindView(R.id.tv_beauty)
    TextView tvBeauty;
    @BindView(R.id.fl_add)
    FrameLayout flAdd;
    @BindView(R.id.fl_go_chat)
    FrameLayout fl_go_chat;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;

    private UserInfoPresenter mUserInfoPresenter;
    private NewFriendInvitePresenter newFriendInvitePresenter;
    private ReportPresenter mReportPresenter;
    private String userID;
    private String type;
    private List<ReportTypeBean> reportTypeBeans;
    private String uid;
    private String name;
    private UserInfo userInfo;

    public static void start(Context activity, String userId, String type) {
        Intent intent = new Intent(activity, MemberMsgActivity.class);
        intent.putExtra("userId", userId);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }


    @Override
    protected void initInject() {
        mUserInfoPresenter = new UserInfoPresenter();
        mReportPresenter = new ReportPresenter();
        newFriendInvitePresenter = new NewFriendInvitePresenter();
        initPresenters(mUserInfoPresenter, mReportPresenter, newFriendInvitePresenter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_msg;
    }

    @Override
    protected void initViewAndData() {
        makeStatusBarTransparent(this);
        ImageLoader.loadCircleHead(this, R.mipmap.default_avatar, ivHead);
        userID = getIntent().getStringExtra("userId");
        type = getIntent().getStringExtra("type");
        if (type.equals(IS_FROM_CHAT)) {
            flAdd.setVisibility(View.GONE);
        }
        mUserInfoPresenter.getUserInfo(userID);
        UserInfo loginUser = UserMgr.getInstance().getLoginUser();
        if (userID.equals(loginUser.getUid())) {
            llBottom.setVisibility(View.GONE);
            tvTopRight.setVisibility(View.GONE);
        }

    }


    public static void makeStatusBarTransparent(Activity activity) {
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }


    @OnClick({R.id.iv_back, R.id.tv_top_right, R.id.tv_add, R.id.tv_gon_chat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_top_right:
                showDialog();
                break;
            case R.id.tv_add:
                showLoadingDialog();
                newFriendInvitePresenter.addFriend(userID);
                break;
            case R.id.tv_gon_chat:// 私聊
                Intent intent = new Intent(this, ChatActivity.class);
                // it's single chat
                intent.putExtra(Constant.EXTRA_USER_ID, uid);
                intent.putExtra("showName", name);
                startActivity(intent);

                break;
        }
    }

    private void showDialog() {
        if (this.userInfo == null) {
            return;
        }
        final String[] data = new String[2];
        data[0] = "举报此人";
        int userRelate = userInfo.getUserRelate();
        if (userRelate != 2) {
            data[1] = "屏蔽此人信息";
        } else {
            data[1] = "取消屏蔽";
        }

        CommonBottomChosenDialog.newInstance()
                .setData(data)
                .setOnItemClickListener(new CommonBottomChosenDialog.onItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        if (position == 0) {
                            if (reportTypeBeans != null && reportTypeBeans.size() > 0) {
                                showJUBAODialog(reportTypeBeans);
                            } else {
                                mReportPresenter.getReportType();
                            }

                        } else {
                            if (userRelate == 2) {
                                mReportPresenter.cancelBlack(userID);
                            } else {
                                mReportPresenter.addBlack(userID);
                            }
                        }
                    }
                })
                .show(getSupportFragmentManager());
    }


    private void showJUBAODialog(List<ReportTypeBean> reportTypeBeans) {
        ArrayList<String> strings = new ArrayList<>();
        for (ReportTypeBean reportTypeBean : reportTypeBeans) {
            strings.add(reportTypeBean.getContent());
        }
        final String[] data = strings.toArray(new String[0]);
        CommonBottomChosenDialog.newInstance()
                .setData(data)
                .setOnItemClickListener(new CommonBottomChosenDialog.onItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        mReportPresenter.reportSomeBody(userID, reportTypeBeans.get(position).getId());
                    }
                })
                .show(getSupportFragmentManager());
    }

    @Override
    public void setUserInfo(UserInfo userInfo) {
        if (userInfo == null)
            return;
        this.userInfo=userInfo;
        uid = userInfo.getUid();
        name = userInfo.getName();
        tvNickName.setText(userInfo.getName());
        tvMvp.setText(getString(R.string.mvp, userInfo.getMvpCount()));
        tvBeauty.setText(getString(R.string.beauty, userInfo.getYzCount()));
        tvCourage.setText(getString(R.string.courage, userInfo.getYqCount()));
        tvWisdom.setText(getString(R.string.wise, userInfo.getZhCount()));
        tvId.setText("ID: " + userInfo.getUid());
        tvSign.setText(userInfo.getSign());
        ivLevel.setText("LV." + userInfo.getLevel());
        if (FlashCodes.SEX_MAN.equals(userInfo.getSex())) {
            ViewUtil.setRightIcon(tvSex, R.mipmap.icon_man_white);
            ViewUtil.setViewShapeColor(tvSex, "#2196F3");
        } else {
            ViewUtil.setRightIcon(tvSex, R.mipmap.icon_woman_white);
            ViewUtil.setViewShapeColor(tvSex, "#EF89D2");
        }

        ImageLoader.loadCircleHead(this, userInfo.getPhoto(), ivHead);
        if (userInfo.getUserRelate() == 0 ) {
            flAdd.setVisibility(View.VISIBLE);
        } else {
            flAdd.setVisibility(View.GONE);
        }
        if (userInfo.getUserRelate() == 1 ) {
            fl_go_chat.setVisibility(View.VISIBLE);
        } else {
            fl_go_chat.setVisibility(View.GONE);
        }
    }

    @Override
    public void setReportTypeData(List<ReportTypeBean> reportTypeData) {
        this.reportTypeBeans = reportTypeData;
        if (reportTypeData != null && reportTypeData.size() > 0) {
            showJUBAODialog(reportTypeBeans);
        }
    }

    @Override
    public void reportSomeBodySuccess() {
        FlashLib.showToast("举报成功");
    }

    @Override
    public void addBlackFriendSuccess() {
        FlashLib.showToast("操作成功");
        mUserInfoPresenter.getUserInfo(userID);
    }

    @Override
    public void cancelBlackFriendSuccess() {
        FlashLib.showToast("操作成功");
        mUserInfoPresenter.getUserInfo(userID);
    }

    @Override
    public void setUi(List<ChatUserBean> chatUserBeans) {

    }

    @Override
    public void addFriendResult(boolean success) {
        dismissLoadingDialog();
    }
}
